{include file="public/header" /}
<link rel="stylesheet" type="text/css" href="/public/static/admin/webupload/webuploader.css">
<link rel="stylesheet" type="text/css" href="/public/static/admin/webupload/style.css">
<style>
    .file-item {
        float: left;
        position: relative;
        width: 110px;
        height: 110px;
        margin: 0 20px 20px 0;
        padding: 4px;
    }

    .file-item .info {
        overflow: hidden;
    }

    .uploader-list {
        width: 100%;
        overflow: hidden;
    }

    .l {
        position: relative;
    }

    .r {
        position: absolute;
        top: 0;
        right: -160px;
        width: 160px;
        height: 38px;
        line-height: 38px;
        padding: 0 0 0 10px;
    }
    .r *{
        user-select: none;
    }
    .r b {
        display: inline-block;
        height: 20px;
        line-height: 20px;
        text-align: center;
        margin: 0 10px 0 0;
        font-weight: normal;
    }

    .r b i {
        font-size: 20px;
    }

    .red {
        color: red;
    }

    .green {
        color: #1AB394;
    }
</style>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>添加会员</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal" name="add_member" id="add_member" method="post"
                          action="{:url('add_member')}">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">会员账号：</label>
                            <div class="input-group col-sm-4 l">
                                <input id="account" type="text" class="layui-input" name="account"
                                       value="{:create_account_only()}" placeholder="请输入账号">

                                <div class="r">
                                    <b>
                                        <i class="fa fa-check green"></i>
                                    </b>
                                    <a onclick="get_account(this,'account')">重新生成</a>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">会员姓名：</label>
                            <div class="input-group col-sm-4">
                                <input id="nickname" type="text" class="layui-input" name="nickname"
                                       placeholder="请输入会员姓名">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">登录密码：</label>
                            <div class="input-group col-sm-4">
                                <input id="password" type="text" class="layui-input" name="password"
                                       value="{:mt_rand(100000,999999)}" placeholder="请输入登录密码">
                                <div class="r">
                                    <b>
                                        <i class="fa fa-check green"></i>
                                    </b>
                                    <a onclick="get_account(this,'password')">重新生成</a>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">安全密码：</label>
                            <div class="input-group col-sm-4">
                                <input id="password2" type="number" class="layui-input" name="security"
                                       value="{:mt_rand(100000,999999)}" placeholder="请输入安全密码">
                                <div class="r">
                                    <b>
                                        <i class="fa fa-check green"></i>
                                    </b>
                                    <a onclick="get_account(this,'password')">重新生成</a>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">性别：</label>
                            <div class="input-group col-sm-4">
                                <div class="radio i-checks">
                                    <input type="radio" name='sex' value="1"/> <i class="fa fa-mars" aria-hidden="true" style="color: #1E88E5;"></i>&emsp;&emsp;&emsp;&emsp;
                                    <input type="radio" name='sex' value="2"/> <i class="fa fa-venus" aria-hidden="true" style="color: hotpink;"></i>&emsp;&emsp;&emsp;&emsp;
                                    <input type="radio" name='sex' value="0" checked="checked"/> <i class="fa fa-mars-double" aria-hidden="true" style="color: #777777;"></i>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">会员组：</label>
                            <div class="input-group col-sm-4">
                                <select class="form-control m-b chosen-select" name="group_id" id="group_id">
                                    <option value="">==请选择会员组==</option>
                                    {if !empty($group)}
                                    {foreach name="group" item="vo"}
                                    <option value="{$vo.id}">{$vo.group_name}</option>
                                    {/foreach}
                                    {/if}
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">会员头像：</label>
                            <div class="input-group col-sm-4">
                                <input type="hidden" id="data_photo" name="head_img"
                                       value="20180104/45c5c22b31dc287543f91cead0c7d894.jpg">
                                <div id="fileList" class="uploader-list" style="float:right"></div>
                                <div id="imgPicker" style="float:left">选择头像</div>
                                <img id="img_data" class="img-circle" height="80px" width="80px"
                                     style="float:left;margin-left: 50px;margin-top: -10px;"
                                     src="/public/uploads/face/20180104/45c5c22b31dc287543f91cead0c7d894.jpg"/>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">手机号码：</label>
                            <div class="input-group col-sm-4">
                                <input id="mobile" type="tel" class="layui-input" name="mobile"
                                       placeholder="请输入会员电话">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">状态：</label>
                            <div class="radio i-checks">
                                <input type="radio"  value="1" name="status" title="">激活
                                &emsp;&emsp;&emsp;&emsp;
                                <input type="radio"  value="0" name="status" title="" checked>未激活
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-3">
                                <button class="btn btn-primary" type="submit"><i class="fa fa-save"></i> 保存</button>&nbsp;&nbsp;&nbsp;
                                <a class="btn btn-danger" href="javascript:history.go(-1);"><i class="fa fa-close"></i>
                                    返回</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script type="text/javascript" src="/public/static/admin/webupload/webuploader.min.js"></script>

<script type="text/javascript">
    var $list = $('#fileList');
    //上传图片,初始化WebUploader
    var uploader = WebUploader.create({

        auto: true,// 选完文件后，是否自动上传。   
        swf: '/public/static/admin/webupload/Uploader.swf',// swf文件路径
        server: "{:url('Upload/uploadface')}",// 文件接收服务端。
        duplicate: true,// 重复上传图片，true为可重复false为不可重复
        pick: '#imgPicker',// 选择文件的按钮。可选。

        accept: {
            title: 'Images',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'image/jpg,image/jpeg,image/png'
        },

        'onUploadSuccess': function (file, data, response) {
            $("#data_photo").val(data._raw);
            $("#img_data").attr('src', '/public/uploads/face/' + data._raw).show();
        }
    });

    uploader.on('fileQueued', function (file) {
        $list.html('<div id="' + file.id + '" class="item">' +
            '<h4 class="info">' + file.name + '</h4>' +
            '<p class="state">正在上传...</p>' +
            '</div>');
    });

    // 文件上传成功
    uploader.on('uploadSuccess', function (file) {
        $('#' + file.id).find('p.state').text('上传成功！');
    });

    // 文件上传失败，显示上传出错。
    uploader.on('uploadError', function (file) {
        $('#' + file.id).find('p.state').text('上传出错!');
    });

    //提交
    $(function () {
        $('#add_member').ajaxForm({
            beforeSubmit: checkForm,
            success: complete,
            dataType: 'json'
        });

        function checkForm() {
            if ('' == $.trim($('#account').val())) {
                layer.msg('请输入账号', {icon: 2, time: 1500, shade: 0.1}, function (index) {
                    layer.close(index);
                });
                return false;
            }

            if ('' == $.trim($('#nickname').val())) {
                layer.msg('请输入昵称', {icon: 2, time: 1500, shade: 0.1}, function (index) {
                    layer.close(index);
                });
                return false;
            }

            if ('' == $.trim($('#password').val())) {
                layer.msg('请输入登录密码', {icon: 2, time: 1500, shade: 0.1}, function (index) {
                    layer.close(index);
                });
                return false;
            }

            if ('' == $.trim($('#password2').val())) {
                layer.msg('请输入安全密码', {icon: 2, time: 1500, shade: 0.1}, function (index) {
                    layer.close(index);
                });
                return false;
            }

            if ($.trim($('#password2').val()).length !=6) {
                layer.msg('安全密码必须是6位数字', {icon: 2, time: 1500, shade: 0.1}, function (index) {
                    layer.close(index);
                });
                return false;
            }

            if ('' == $.trim($('#group_id').val())) {
                layer.msg('请选择会员组', {icon: 2, time: 1500, shade: 0.1}, function (index) {
                    layer.close(index);
                });
                return false;
            }

        }


        function complete(data) {
            if (data.code == 1) {
                layer.msg(data.msg, {icon: 6, time: 1500, shade: 0.1}, function (index) {
                    window.location.href = "{:url('member/index')}";
                });
            } else {
                layer.msg(data.msg, {icon: 5, time: 1500, shade: 0.1});
                return false;
            }
        }

    });

    function get_account(obj, type) {
        var $this = $(obj);
        $this.siblings('b').html('<i class="fa fa-spinner fa-spin"></i>');
        $this.text('生成中……');
        $.post("{:url('Commen/create_account')}", {type: type || 'account'}, function (result) {
            if (result.code === 200) {
                $this.siblings('b').html('<i class="fa fa-check green"></i>');
                $this.parent().siblings('input').val(result.data);
            } else {
                $this.siblings('b').html('<i class="fa fa-times red"></i>');
            }
            $this.text('重新生成');
        })
    }


    //IOS开关样式配置
    var elem = document.querySelector('.js-switch');
    var switchery = new Switchery(elem, {
        color: '#1AB394'
    });
    var config = {
        '.chosen-select': {}
    };
    for (var selector in config) {
        $(selector).chosen(config[selector]);
    }

</script>
</body>
</html>